
package com.zsrt.day20170427.ch01;

import java.lang.reflect.Method;

import org.junit.Test;

/**
 * com.zsrt.day20170427.ch01.Student
 * 
 * getmethods 拿到这个类的公有方法 包括父类的方法
 * 
 * method 调用方法必须回溯 使用 invoke
 * 
 * getDeclaredMethods 拿到自己的方法 构造方法除外
 * @author jmj
 * @date 2017年4月27日
 * @time 上午10:42:29
 */
public class MethodTest {

	
	@Test
	public void test01() throws Exception {
		
		//得到Class 
		Class clazz = Class.forName("com.zsrt.day20170427.ch01.Student");
		
		//公有方法
		Method[] methods = clazz.getMethods();
		for(Method m:methods){
			System.out.println(m.getName());
		}
		//还包括父类object
		System.out.println(methods.length);
	}
	
	@Test
	public void test02() throws Exception {
		
		//得到Class 
		Class clazz = Class.forName("com.zsrt.day20170427.ch01.Student");
		
		//调用的默认的无参构造方法
		Student student = (Student)clazz.newInstance();
		
		//Method[] methods = clazz.getMethods();
		
		//还包括父类object
		//System.out.println(methods.length);
		
		//自己的方法包括私有方法
		Method[] methods = clazz.getDeclaredMethods();
		for(Method m:methods){
			System.out.println(m.getName());
		}
		System.out.println(methods.length);
	}
	
	@Test
	public void test03() throws Exception {
		
		//得到Class 
		Class clazz = Class.forName("com.zsrt.day20170427.ch01.Student");
		
		Student student = (Student)clazz.newInstance();
		
		Method[] methods = clazz.getMethods();
		
		//还包括父类object
		System.out.println(methods.length);
		
		//私有方法
		//clazz.getDeclaredMethods();
		
		//Method method = clazz.getMethod("add", int.class,int.class  );
		
		//强制打开
		//method.setAccessible(true);
		
		//Object object = method.invoke("add", 10,10);
		//System.out.println(object);
	}
	
	/**
	 * 拿到公有方法
	 * @throws Exception
	 */
	@Test
	public void test04() throws Exception {
		
		//得到Class 
		Class clazz = Class.forName("com.zsrt.day20170427.ch01.Student");
		
		Student student = (Student)clazz.newInstance();
		
		Method[] methods = clazz.getMethods();
		
		//还包括父类object
		//System.out.println(methods.length);
		
		//私有方法
		//clazz.getDeclaredMethods();
		//强制打开
		
		//Method method = clazz.getMethod("add", int.class,int.class  );
		for(Method method:methods){
			String methodName  =method.getName();
			
			if("add".equals(methodName)){
				//如何调用add方法
				Object object = method.invoke(student, 10,20);
				System.out.println(object);
			}
		}
		//method.setAccessible(true);
		
		//Object object = method.invoke("add", 10,10);
		//System.out.println(object);
			
	}
	
	
	@Test
	public void test05() throws Exception {
		
		Class clazz = Class.forName("com.zsrt.day20170427.ch01.Student");
		Student student = (Student) clazz.newInstance();
		//调用setage
		Method setMethod = clazz.getDeclaredMethod("setAge", int.class);
		
		setMethod.invoke("setAge", 22);
		//在类的对象上调用  方法名.invoke(对象，参数)
		
		//调用getage方法
		
		Method getMethod = clazz.getDeclaredMethod("getAge", null);
		//getMethod.setAccessible(true);
		Object object2 = getMethod.invoke(student, null);
		System.out.println(object2);
	}
	
	/**
	 * 调用私有方法
	 * @throws Exception
	 */
	@Test
	public void test06() throws Exception {
		
		//1 得到Class 照妖镜

		Class clazz = Class.forName("com.zsrt.day20170427.ch01.Student");
		
		Student student = (Student) clazz.newInstance();
		
		//拿到私有的方法

		Method addMethod = clazz.getDeclaredMethod("add", int.class,int.class);
		System.out.println(addMethod);
		
		addMethod.setAccessible(true);
		
		Object object = addMethod.invoke(student, 200,300);
		System.out.println(object);
		
		
		// STUDENT_AGE  setStudentAge

		//student.add(200, 300);

		
		//调用setStudentAge?

		//1 拿到方法public  void setStudentAge(int studentAge)

		Method setStudentAgeMethod =clazz.getDeclaredMethod("setAge", int.class);
		//2 在类的对象上调用

		//对象.方法名(参数)  方法名.invoke(对象,参数)

		setStudentAgeMethod.invoke(student, 33);
		
		// getStudentAge

		Method getStudentAgeMethod = clazz.getDeclaredMethod("getAge", null);
		Object jieguo  =getStudentAgeMethod.invoke(student, null);
		System.err.println(jieguo);
		
		System.out.println(student);
	}
	@Test
	public void test77() throws Exception {
		
		//1 得到Class 照妖镜
		Class clazz = Class.forName("com.zsrt.day20170427.ch01.Student");
	
		//自己的方法 构造方法除
		System.out.println(clazz.getDeclaredMethods().length);
	}
}
